using System;
using AwesomeEnginePC.Graphics.TwoDimensional.Sprites;
using AwesomeEnginePC.Inputs;
using AwesomeEnginePC.Physics;
using AwesomeEnginePC.Timing;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace NoNameGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;

        //The player
        

        private SpriteSheet regSpriteSheet;
        private SpriteSheet CustomSpriteSheet;

        private BaseSprite simpleSprite;

        private TimeSpan prevTime;
        private TimingSystem shortTiming;
        private TimingSystem longTiming;
        private bool isFirstUpdate;

        private InputManager inputManager;
        private bool isSimpleSpriteRed;

        private PixelCollision Collision;
        private bool isHit = false;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "AE.Content.ContentCommon";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
            shortTiming = new TimingSystem();
            longTiming = new TimingSystem();
            isFirstUpdate = true;
            isSimpleSpriteRed = false;
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            regSpriteSheet = new SpriteSheet(Content, "sprites/spritesheet1", 100, 100);
            regSpriteSheet.Position = new Vector(550, 50);
            //regSpriteSheet.ScalingX = 0.5f;
            //regSpriteSheet.ScalingY = 0.5f;

            CustomSpriteSheet = new SpriteSheet(Content, "sprites/spritesheet1", 100, 100);
            CustomSpriteSheet.Position = new Vector(150, 150);
            CustomSpriteSheet.InitalizeForCustomSpriteSheet();
            CustomSpriteSheet.CustomSheetList.Add(new Rectangle(0, 0, 100, 100));
            CustomSpriteSheet.CustomSheetList.Add(new Rectangle(100, 100, 100, 100));
            CustomSpriteSheet.CustomSheetList.Add(new Rectangle(200, 100, 100, 100));
            CustomSpriteSheet.CustomSheetList.Add(new Rectangle(300, 300, 100, 100));
            CustomSpriteSheet.CustomSheetList.Add(new Rectangle(200, 200, 100, 100));
            CustomSpriteSheet.CustomSheetList.Add(new Rectangle(100, 300, 100, 100));

            simpleSprite = new BaseSprite(Content, "poker");
            simpleSprite.ScalingX = 0.5f;
            simpleSprite.ScalingY = 0.5f;

            //simpleSprite.Origin = simpleSprite.CenterPoint;
            simpleSprite.Position = new Vector(200, 200);

            Collision = new PixelCollision();

            inputManager = new InputManager(true, true, false);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            if (isFirstUpdate)
            {
                isFirstUpdate = false;
                shortTiming.SetTiming(new TimeSpan(0, 0, 0, 1, 0), gameTime.TotalGameTime);
                longTiming.SetTiming(new TimeSpan(0, 0, 0, 15, 0), gameTime.TotalGameTime);
            }

            if (prevTime == null)
            {
                prevTime = gameTime.TotalGameTime;
            }

            // TODO: Add your update logic here
            inputManager.Update(gameTime);

            regSpriteSheet.Update(gameTime);
            CustomSpriteSheet.Update(gameTime);
            simpleSprite.Update(gameTime);
            isHit = false;
            if (inputManager.MouseButtonStates[MouseButtons.LeftClick] == ButtonState.Released && inputManager.KeyboardKeyStates[Keys.T] == ButtonState.Released)
            {
                //player.Rotation += MathHelper.PiOver4 / 60;
            }
            else
            {

                CustomSpriteSheet.Position.X -= 2;
                //player.Rotation -= MathHelper.PiOver4 / 60;
            }
            if (inputManager.MouseButtonStates[MouseButtons.RightClick] == ButtonState.Pressed)
            {
                CustomSpriteSheet.Position.X += 2;
            }

            if (inputManager.JustPressedMouseButtons.Contains(MouseButtons.RightClick) || inputManager.JustPressedKeyboardKeys.Contains(Keys.R))
            {
                //isSimpleSpriteRed = !isSimpleSpriteRed;
            }

            if (Collision.CollidesWith(CustomSpriteSheet, simpleSprite))
            {
                isHit = true;
            }

            if (shortTiming.HasTargetTimeElapsed(gameTime.TotalGameTime))
            {
                if (CustomSpriteSheet.CustomSheetIndex < CustomSpriteSheet.CustomSheetCount)
                {
                    CustomSpriteSheet.CustomSheetIndex++;
                }
                else
                {
                    CustomSpriteSheet.CustomSheetIndex = 0;
                }
                regSpriteSheet.SequentialFrameChange(1);
               
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            // Change the background to red when the person was hit by a block

            if (isHit)
            {
                GraphicsDevice.Clear(Color.Red);
            }
            else
            { 
                GraphicsDevice.Clear(Color.CornflowerBlue);
            }
           

            // TODO: Add your drawing code here
            spriteBatch.Begin();
           
            regSpriteSheet.Draw(gameTime, spriteBatch);
            CustomSpriteSheet.Draw(gameTime, spriteBatch);
            simpleSprite.Draw(gameTime, spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}